<?php
namespace app\common\model;
use x\Xbase;
class Dianzan extends Xbase
{

    /**
     * 点赞
     * @param  [type] $user_id  [会员id]
     * @param  [type] $table    [数据表]
     * @param  [type] $table_id [数据表id]
     * @return [type]           [description]
     */
    public function dz($user_id, $table, $table_id){
        $map = [
            ['and','user_id','=',$user_id],
            ['and','table','=',$table],
            ['and','table_id','=',$table_id],
            ['and','type','=',0],
        ];
        $w = where($map);
        $rs = $this->db->query("select id from dianzan $w[0]",$w[1],1);
        if($rs){
            $this->db->delete('dianzan',$rs['id']);
        }else{
            $d = [
                'user_id' => $user_id,
                'table' => $table,
                'table_id' => $table_id,
                'type' => 0,
                'ip' => ip(),
            ];
            $this->db->insert('dianzan',$d);
        }
        return true;
    }

    //用户的点赞
    public function userDz($user_id, $table){
        $rs = $this->db->query('select table_id from dianzan where user_id=? and `table`=?',[$user_id,$table]);
        $temp = [];
        foreach($rs as $v){
            $temp[] = $v['table_id'];
        }
        return $temp;
    }
}
